package com.hz.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hz.web.entity.CkHtmlEntity;
import com.hz.web.entity.MenuEntity;
import com.hz.web.entity.RoleMenuEntity;
import com.hz.web.entity.UserEntity;
import com.hz.web.service.IckHtmlService;
import com.hz.web.service.ImenuService;
import com.hz.web.service.IroleMenuService;
import com.hz.web.util.Constant;
import com.hz.web.util.UtilMethods;


@Controller
@RequestMapping("menu")
public class MenuController {

	@Resource
	private ImenuService menuService;//菜单管理
	
	@Resource
	private IroleMenuService roleMenuService;//权限菜单
	@Resource
	private IckHtmlService service;
	
	/**
	 * 根据富文本类型查询菜单
	 * @return
	 */
	@RequestMapping("queryMsgByCktype")
	@ResponseBody
	public Map<String, Object> queryMsgByCktype(String ckType){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		List<MenuEntity> lsEntity = new ArrayList<MenuEntity>();
		if(ckType !=null && !"".equals(ckType.trim())){
			String houUrl = "%ckType="+ckType.trim()+"%";
			parmsMap.put("houUrl", houUrl);
			lsEntity = menuService.queryNodesTree(parmsMap);
		}
		resultMap.put("lsEntity", lsEntity);
		return resultMap;
	}
	
	/**
	 * 查询联系我们
	 * @param id
	 * @param parentId
	 * @return
	 */
	@RequestMapping("queryContact")
	@ResponseBody
	public Map<String, Object> queryContact(Integer parentId,String sort){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		List<MenuEntity> result = new ArrayList<MenuEntity>();
		parmsMap.put("menuId", parentId);//当前菜单
		parmsMap.put("sort", sort);
		result = menuService.queryNodesTreeOnly(parmsMap);
		if(result.size()>0){
			resultMap.put("parentMenu", result.get(0));
		}
		parmsMap.put("menuId", null);
		parmsMap.put("id", parentId);//父级菜单下的所有二级菜单集合
		result = menuService.queryNodesTreeOnly(parmsMap);
		if(result.size()>0){
			for (MenuEntity menuEntity : result) {
				List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>();
				 String houUrl=menuEntity.getHouUrl();
				 String[] shuzu =new String[5];
				 shuzu = houUrl.split("ckType=");
				  String cktype = "";
				   if(shuzu.length>=2){
					   cktype=shuzu[1];
					   String[] shuzu2 = new String[5];;
					   shuzu2 = cktype.split("&");
					   if(shuzu2.length>=1){
						   cktype=shuzu2[0];
					   }
				   }
				   int ck=0;
				   if(!"".equals(cktype)){
					   ck = Integer.valueOf(cktype);
				   }
				list = queryCkHtml(ck,sort);
				menuEntity.setCklist(list);
			}
			resultMap.put("menuList", result);
		}
		return resultMap;
	}
	public List<CkHtmlEntity> queryCkHtml(int ckType,String sort){
		Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
		parmsMap.put("state", "1");
		parmsMap.put("ckType", ckType);//富文本类型   判断是哪个功能的富文本信息
		parmsMap.put("sort", sort);
		//根据参数map查询出的富文本编辑的结果集合
		List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>();
		list =service.queryCkHtml(parmsMap);
		String htmlName="";
		htmlName=UtilMethods.getNameByHtmlType(ckType);
		for (CkHtmlEntity ckHtml : list) {
			ckHtml.setTypeName(htmlName);
		}
		return list;
	}
	
	/**
	 * 查询二级页面的菜单信息
	 * @param id
	 * @param parentId
	 * @return
	 */
	@RequestMapping("querySecondMenuQian")
	@ResponseBody
	public Map<String, Object> querySecondMenuQian(Integer parentId,String sort){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		List<MenuEntity> result = new ArrayList<MenuEntity>();
		parmsMap.put("menuId", parentId);//当前菜单
		parmsMap.put("sort", sort);
		result = menuService.queryNodesTreeOnly(parmsMap);
		if(result.size()>0){
			resultMap.put("parentMenu", result.get(0));
		}
		parmsMap.put("menuId", null);
		parmsMap.put("id", parentId);//父级菜单下的所有二级菜单集合
		parmsMap.put("yseno", "1");
		
		result = menuService.queryNodesTreeOnly(parmsMap);
		if(result.size()>0){
			resultMap.put("menuList", result);
		}
		return resultMap;
	}
	
	/**
	 * 前台查询菜单
	 * @param id
	 * @return
	 */
	@RequestMapping("queryMenuFromQian")
	@ResponseBody
	public List<MenuEntity> queryMenuFromQian(int parentId){	
		List<MenuEntity> result = new ArrayList<MenuEntity>();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		parmsMap.put("id", parentId);
		parmsMap.put("yseno", 1);//1是在前台显示的  0是不显示的
		//result = menuService.queryNodesTree(parmsMap);
		result = menuService.queryNodesTreeOnly(parmsMap);
		return result;
	}
	
	
	/**
	 * 跳转到指定的页面
	 * @param toUrl
	 * @return
	 */
	@RequestMapping("toPage")
	public String toPage(String urlStr){
		return urlStr;
	}
	
	
	/**
	 * 根据父id查询子菜单   权限控制菜单 ，主要是  1:未分配权限 2:已分配权限的   查询
	 * @param id
	 * @return
	 */
	@RequestMapping("queryMenuByRole")
	@ResponseBody
	public List<MenuEntity> queryMenuByRole(int id,String urlType,int roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		
		List<RoleMenuEntity> list = queryRoleMenuByRole(roleId);//根据权限查询到的 (权限菜单) 信息
		
		if(list.size()==0){
			RoleMenuEntity menu = new RoleMenuEntity();
			menu.setMenuId(-1);//如果没有查到，就给个默认值，这个默认值在数据库是查不到的
			list.add(menu);
		}
		map.put("urlType", urlType);//区分  前台  传过来的路径  是查询未分配还是已分配的菜单
		map.put("list", list);
		
		if(Constant.MENU_ROLE_URL_TYPE_NO.equals(urlType)){//未分配权限的
			List<MenuEntity> ls = menuService.queryNodesNoTree(map);//查询到的所有不在已分配菜单里面的  菜单集合
			List<Integer> noAddls =  queryNoAddMenu(ls );
			
			Map<String, Object> mapNoAdd = new HashMap<String, Object>();
			//重新封装查询参数   这是查询未分配菜单的
			mapNoAdd.put("id",id);
			mapNoAdd.put("urlType", urlType);
			mapNoAdd.put("noAddls", noAddls);
			
			List<MenuEntity> listNoAdd = menuService.queryNodesTree(mapNoAdd);
			
			return listNoAdd;
		}
		//这是查询已分配菜单的，id不能写在上面，只能写在这里，不然会影响 未分配菜单的查询
		map.put("id",id);
		
		List<MenuEntity> ls = menuService.queryNodesTree(map);
		
		return ls;
		
	}
	/**
	 * 查询出权限分配里面，未分配的菜单集合 resultList 主要是查询父节点
	 * @return
	 */
	public List<Integer> queryNoAddMenu(List<MenuEntity> ls ){
		//参数ls是查询到的，未分配菜单集合，里面没有包含父节点，所有需要这个方法重新控制
		List<Integer> resultList = new ArrayList<Integer>();//最后的结果集合
		
		
		for (MenuEntity menu : ls) {
			int id = menu.getId();
			int parentId =menu.getParentId();
			if(isExitToList( resultList,id)==false){//判断id在集合存在不存在，如果不存在就存进集合
				resultList.add(id);
			}
			
			while (true) {
				if(isExitToList( resultList,parentId)==true){//判断父id在集合存在不存在，如果存在就跳出循环
					break;
				}
				if(isExitToList( resultList,parentId)==false){//判断父id在集合存在不存在，如果不存在就存进集合
					resultList.add(parentId);
				}
				
				if(Constant.MENU_PARENT_GEN==parentId){//如果当前菜单的父id是0，就说明到根目录了，就跳出循环
					break;
				}else{
					Map<String, Object> mapUpParent = new HashMap<String, Object>();
					mapUpParent.put("menuId", parentId);
					List<MenuEntity> lsParent = menuService.queryNodesNoTree(mapUpParent);
					parentId =lsParent.get(0).getParentId();//根据当前(父id--->parentId)查询出来的(新菜单的父id)
					
				}
			}
		}
		return resultList;
	}
	
	/**
	 * 判断一个数字在一个集合里面存在不存在
	 * @param resultList
	 * @param num
	 * @return
	 */
	public boolean isExitToList(List<Integer> resultList,int num){
		boolean isExit = false;
		for (Integer integer : resultList) {
			if(num==integer){
				isExit=true;
				break;
			}
		}
		return isExit;
	}
	
	/**
	 * 根据权限id查询出当前权限下面的菜单id
	 * @return
	 */
	public List<RoleMenuEntity> queryRoleMenuByRole(int roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		//int role = 1;//测试权限，成功后记得删除
		map.put("roleId", roleId);
		List<RoleMenuEntity> list = roleMenuService.queryRoleMenu(map);

		return list;
		
	}
	
	
	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping("updateMenu")
	@ResponseBody
	public Object updateMenu(MenuEntity menu){
	
		int rows = menuService.updateMenu(menu);
		
		return rows;
	}
	
	/**
	 * 添加菜单
	 * 
	 */
	@RequestMapping("saveMenu")
	@ResponseBody
	public Object saveMenu(MenuEntity menu){	
		int rows = menuService.saveMenu(menu);
		return rows;
	}
	
	/**
	 * 菜单管理页面
	 */
	@RequestMapping("getAllMenu")
	@ResponseBody
	public List<MenuEntity> getAllMenu(Integer id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id",id);
		List<MenuEntity> list = menuService.queryNodesTree(map);
		return list;
	}
	

	
	/**
	 * 根据url地址，打开页面
	 * @param houUrl
	 * @return
	 */
	@RequestMapping("openMenu")
	public String openMenu(String houUrl){
		return houUrl;
	}
	
	
	/**
	 * 进入菜单页面，同时查出父菜单
	 * @param model
	 * @return
	 */
	/*@RequestMapping("queryMenu")
	public String queryMenu(Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parentId", Constant.MENU_PARENT_FIRST);
		map.put("roleId",1);
		List<Menu> ls = menuService.queryNodes(map);//只获取第一级目录
		model.addAttribute("ls", ls);
		return "index";
		
	}*/
	/**
	 * 根据父id查询子菜单
	 * @param id
	 * @return
	 */
	@RequestMapping("queryMenuTree")
	@ResponseBody
	public List<MenuEntity> queryMenuTree(HttpSession session,int id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id",id);	
		if(session.getAttribute("userinfo")!=null){
			UserEntity user = (UserEntity) session.getAttribute("userinfo");
			map.put("roleId",user.getRoleId());
		}
		List<MenuEntity> ls = menuService.queryNodesTree(map);
		return ls;
		
	}


}
